<?php

/* Standard function naming
 * get_type_function(udnurl)
 * type is like book, movie, music, or it can be generic for all
 * function is like similar,gallery,links,youtube,etc..
 *
 * for general use generic as type
 */

class scripts
{
    /* GENERIC FUNCTIONS FOR SEO and PRESENTATION */

    public static function get_generic_keywords_links($folder_url, $udnurl, $title, $keywords)
    {
        //use to show extra links with each keyword in 'read' page
        $urls = array();
        if($keywords)
        {
            $pcs = explode('-', $keywords);
            $i = 0;
            foreach($pcs as $keyword)
            {
                $urls[$i]['title'] = util::make_title($title.' '.$keyword);
                $urls[$i]['url'] = $folder_url.$udnurl.'/'.$keyword;
                $i++;
            }
        }

        return $urls;
    }

    public static function get_generic_similar_links($folder_url, $type)
    {
        //TODO: Ensure logic for new table per type model
        //return 10 random links from our sitemap
        $con = util::open_connection();
        $total_rows = 0;
        $result = mysql_query("   SELECT TABLE_ROWS FROM information_schema.tables
                        WHERE TABLE_SCHEMA = 'udnfind'
                        AND TABLE_NAME = 'sitemap_data_$type';");
        while ($row = mysql_fetch_array($result))
        {
            $total_rows = $row[0];
        }

        $random = rand(0, $total_rows - 10);
        $upto = $random + 10;
        $type = mysql_real_escape_string($type);
        $links = array();
        $result = mysql_query(" SELECT * FROM sitemap_data_$type
                                INNER JOIN sitemap_meta ON (sitemap_data_$type.sitemap_meta_id = sitemap_meta.sitemap_meta_id)
                                LEFT JOIN type_meta ON sitemap_meta.type = type_meta.type
                                WHERE sitemap_data_$type.uid > $random AND sitemap_data_$type.uid < $upto
                                LIMIT  10"); //optimized for large data
        $i=0;
        while ($row = mysql_fetch_array($result))
        {
            $links[$i]['title'] = util::make_title($row['title'].' '.$row[17]);
            $links[$i]['url'] = $folder_url.$row['udnurl'].'/'.$row['url_keywords'];
            $i++;
        }

        util::close_connection($con);

        return $links;

    }

    public static function get_generic_content($folder_url, $type, $title, $udnurl)
    {
        //get some text from bing
        $data = services::get_sitemap_data($type, $udnurl);
        $title = trim($data[16].' '.$title.' '.$data[17]);

        //http://www.bing.com/search?q=Graham+Howell+music+albums
        $title = str_replace(' ','+',$title);
        $title = str_replace('-','+',$title);
        $content = util::read_url("http://www.bing.com/search?q=".$title);

        $html = new DOMDocument();
        @$html->loadHTML($content);
        $domxpath = new DOMXPath( $html );

        //cleanup

        //title @h3 a
        $titles = $domxpath->query("//h3//a");
        foreach($titles as $t)
        {
            $urltitle = $t->nodeValue;
            $urltitle = str_replace('…', '', $urltitle);
            $urltitle = str_replace('!', '', $urltitle);
            $urltitle = str_replace(':', '', $urltitle);
            $urltitle = str_replace('|', '', $urltitle);
            $urltitle = str_replace('-', '', $urltitle);
            $urltitle = str_replace(',', '', $urltitle);
            $urltitle = str_replace(' ', '-', $urltitle);
            $urltitle = str_replace('--', '-', $urltitle);
            $urltitle = strtolower($urltitle);
            $urltitle = utf8_encode($urltitle);
            $t->setAttribute('href',$folder_url.'r/'.$type.'/'.$urltitle);
        }

        //remove cite
        util::removeAllElements($domxpath, "//cite");

        //remove translate
        util::removeAllElements($domxpath, "//div[@class='sb_tsuf']");

        $output = $domxpath->query('//div[@id="results"]')->item(0);
        if($output)
            return $html->saveXML($output);
        else
            return null;
    }

    public static function get_generic_description($type, $title)
    {
        $content = scripts::get_generic_content('', $type, $title,'');

        if($content)
        {
            $html = new DOMDocument();
            @$html->loadHTML($content);
            $domxpath = new DOMXPath( $html );

            //remove headings and leave paragraphs only

            $paragraphs = $domxpath->query("//div[@class='sa_mc']//p");
            $output = "";


            $i = 0;
            $max = 3;
            foreach($paragraphs as $p)
            {
                if($i > $max)
                    break;
                $output.='<p>'.utf8_decode($p->nodeValue).'</p>';
                $i++;
            }

            return $output;
        }
        else
            return null;
    }

    public static function get_generic_images($title)
    {
        //get 3 images from google
        //http://www.google.com.sa/search?num=3&hl=en&safe=active&sout=1&tbm=isch&q=phpstorm
        $title = str_replace('-','+',$title);
        $content = util::read_url("https://www.google.com.sa/search?num=3&hl=en&safe=active&sout=1&tbm=isch&q=$title");

        $html = new DOMDocument();
        @$html->loadHTML($content);
        $domxpath = new DOMXPath( $html );

        $imageSet = $domxpath->query("//table[@class='images_table']//img");
        $images = array();
        $i = 0;
        foreach($imageSet as $img)
        {
            $images[$i] = $img->getAttribute('src');
            $i++;
        }
        if($i > 0)
            return $images;
        else
            return null;
    }

    public static function get_generic_poster($title, $type)
    {
        //get 1 image from google
        //http://www.google.com.sa/search?num=3&hl=en&safe=active&sout=1&tbm=isch&tbs=isz:ex,iszw:640,iszh:200&q=phpstorm
        $title = str_replace('-','+',$title);
        $content = util::read_url("https://www.google.com.sa/search?num=5&hl=en&safe=active&sout=1&tbm=isch&tbs=isz:ex,iszw:640,iszh:200&q=$title+$type");

        $html = new DOMDocument();
        @$html->loadHTML($content);
        $domxpath = new DOMXPath( $html );

        $imageLoc = $domxpath->query("//table[@class='images_table']//a");
        $poster = null;
        if($imageLoc->length > 0)
        {
            $imagePage = util::read_url($imageLoc->item(0)->getAttribute('href'));
            $html2 = new DOMDocument();
            @$html2->loadHTML($imagePage);
            $domxpath2 = new DOMXPath( $html2 );

            $poster = $domxpath2->query("//ul[@class='il_ul']//a")->item(0)->getAttribute('href');
        }
        return $poster;
    }




    /* CUSTOM FUNCTIONS FOR RICH CONTENT */

}

?>